function checkErrorJson(data) {
    var result = false;
    if (data != null) {
        if (data._ERROR_MESSAGE_LIST_ == null && data._ERROR_MESSAGE_ == null) {
            result = true;
        } else {
            var errorString = "";
            if (data._ERROR_MESSAGE_LIST_ != null) {
                var errorArray = data._ERROR_MESSAGE_LIST_;
                for (var index = 0; index < errorArray.length; index++) {
                    if (index == 0) {
                        errorString = errorArray[index];
                    } else {
                        errorString = errorString + "\n" + errorArray[index];
                    }
                }
            } else if (data._ERROR_MESSAGE_ != null) {
                errorString = data._ERROR_MESSAGE_;
            }
            alert(errorString);
        }
    } else {
        alert("Return data is empty");
    }
    return result
}
function checkEventJson(data) {
    var result = false;
    if (data != null) {
        if (data._EVENT_MESSAGE_LIST_ == null && data._EVENT_MESSAGE_ == null) {
            result = true;
        } else {
            var eventString = "";
            if (data.__EVENT_MESSAGE_LIST_ != null) {
                var eventArray = data._EVENT_MESSAGE_LIST_;
                for (var index = 0; index < eventArray.length; index++) {
                    if (index == 0) {
                        eventString = eventArray[index];
                    } else {
                        eventString = eventString + "\n" + eventArray[index];
                    }
                }
            } else if (data._EVENT_MESSAGE_ != null) {
                errorString = data._EVENT_MESSAGE_;
            }
            alert(errorString);
        }
    } else {
        alert("Return data is empty");
    }
}

function waitSpinnerShow () {
    jSpinner = jQuery(document.getElementById("wait-spinner"));
    if (!jSpinner.length) return
    bdy = document.body;
    lookupLeft = (bdy.offsetWidth / 2) - (jSpinner.width() / 2);
    scrollOffY = jQuery(window).scrollTop();
    winHeight = jQuery(window).height();
    lookupTop = (scrollOffY + winHeight / 2) - (jSpinner.height() / 2);
    jSpinner.css("left", lookupLeft + "px"); 
    jSpinner.css("top", lookupTop + "px");
    jSpinner.show();
}

function waitSpinnerHide() {
    jQuery(document.getElementById("wait-spinner")).hide();
}
function showDialog(id, url, height, width, title, message, zIndex) {
    var dialog = $("#" + id);
    if ($("#" + id).length == 0) {
        dialog = $('<div id="'+ id +'" style="display:hidden"></div>').appendTo('body');
    }
    dialog.prop("forcedClosing", "false");
    // load remote content
    dialog.load(url, {}, function(responseText, textStatus, XMLHttpRequest) {
        dialog.dialog( {
            resizable : true,
            modal : true,
            width : width,
            height : height,
            title : title,
            zIndex : zIndex,
            beforeClose : function(event, ui) {
                if(message!=null && ($("#dialog").prop("forcedClosing") == 'false')){
                alert(message);
                return false;
                }
            },
            close: function(event, ui) {
                $(this).dialog('destroy').remove();
            }
        });
    });
    return false;
}

function salesGPS(formObj) {
    waitSpinnerShow();
    jQuery.ajax({
        type: "POST",
        url: $(formObj).attr("action"),
        data: $(formObj).serialize(),
        error: function(msg) {
            waitSpinnerHide();
            alert("An error occurred loading content! : " + msg);
        },
        success: function(data) {
            waitSpinnerHide();
            if (checkErrorJson(data)) {
                checkEventJson(data);
                $("#SalesGPSDialog").dialog("close");
                updateDealerTreeSection();
                ajaxUpdateArea("righthalf", "ViewCustomer", "partyId=" + formObj.partyId.value);
                setTimeout(function() {
                    jQuery("#tree").jstree("open_node", "#" + data.partyIdFrom);
                }, 2000);
            }
        }
    });
    return false;
}

function createCustomer(formObj) {
    waitSpinnerShow();
    jQuery.ajax({
        type: "POST",
        url: $(formObj).attr("action"),
        data: $(formObj).serialize(),
        error: function(msg) {
            waitSpinnerHide();
            alert("An error occurred loading content! : " + msg);
        },
        success: function(data) {
            waitSpinnerHide();
            if (checkErrorJson(data)) {
                checkEventJson(data);
                $("#NewCustomerDialog").dialog("close");
                updateDealerTreeSection();
                ajaxUpdateArea("righthalf", "ViewCustomer", "partyId=" + data.partyId);
                setTimeout(function() {
                    jQuery("#tree").jstree("open_node", "#" + data.partyIdFrom);
                }, 2000);
            }
        }
    });
    return false;
}
function moveCustomer(formObj) {
    waitSpinnerShow();
    jQuery.ajax({
        type: "POST",
        url: $(formObj).attr("action"),
        data: $(formObj).serialize(),
        error: function(msg) {
            waitSpinnerHide();
            alert("An error occurred loading content! : " + msg);
        },
        success: function(data) {
            waitSpinnerHide();
            if (checkErrorJson(data)) {
                checkEventJson(data);
                $("#MoveCustomerDialog").dialog("close");
                updateDealerTree();
            }
        }
    });
    return false;
}

function deleteCustomer(partyId) {
    waitSpinnerShow();
    jQuery.ajax({
        type: "POST",
        url: "deleteCustomer",
        data: {"partyId" : partyId},
        error: function(msg) {
            waitSpinnerHide();
            alert("An error occurred loading content! : " + msg);
        },
        success: function(data) {
            waitSpinnerHide();
            if (checkErrorJson(data)) {
                checkEventJson(data);
                updateDealerTree();
            }
        }
    });
    return false;
}
